//
//  AppDelegate.m
//  PhoneCallMonitor
//
//  Created by paycloud110 on 15/12/16.
//  Copyright © 2015年 paycloud110. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundUpdateTask;
@property (nonatomic, strong) NSTimer *backgroundTimer;

/**  */
@property (nonatomic, assign) NSUInteger *counter;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        [self registerAuthor];
    }
    return YES;
}

-(void)applicationDidBecomeActive:(UIApplication *)application
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
/**
 *  当用户接收到通知, 并且
 1. 当app, 在前台时
 2. 当app在后台时, 点击通知
 3. 锁屏状态, 滑动通知, 打开app
 4. 当app, 被彻底关闭, 会不会打印?
 答案: 不会执行这个方法, 需要到 didFinishLaunchingWithOptions 方法里面接收通知
 *
 *  @param application  应用程序
 *  @param notification 通知
 */
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"接收到通知");
    NSLog(@"%zd", application.applicationState);
    if(application.applicationState == UIApplicationStateActive)
    {
        NSLog(@"通过数字来提醒用户");
    }else if (application.applicationState == UIApplicationStateInactive)
    {
        NSLog(@"跳转新界面");
    }
}

/**
 *  用户点击快捷操作组的调用
 */
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    NSLog(@"----%@", identifier);
    // 系统检测, 看用户到底有没有完成处理
    completionHandler();
}

#pragma mark -私有方法
- (void)registerAuthor
{
    // 创建一个快捷操作组 NSSet<UIUserNotificationCategory *>
    
    UIMutableUserNotificationCategory  *cate = [[UIMutableUserNotificationCategory   alloc]  init];
    // 用来多个操作组之间的识别
    cate.identifier = @"select";
    
    // 设置操作组的操作行为 NSArray<UIUserNotificationAction *>
    // 创建一个操作项
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction   alloc]  init];
    // 设置操作标识
    action1.identifier = @"king";
    // 设置行为名称
    action1.title = @"王炸";
    // 设置行为的执行模式
    action1.activationMode = UIUserNotificationActivationModeForeground; // 前台执行
    
    // 创建一个操作项
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction   alloc]   init];
    // 设置操作标识
    action2.identifier = @"pass";
    // 设置行为名称
    action2.title = @"要不起";
    // 设置行为的执行模式
    action2.activationMode = UIUserNotificationActivationModeBackground; // 后台执行
    
    NSArray *actions = @[action1, action2];
    
    [cate     setActions:actions forContext:UIUserNotificationActionContextMinimal];
    
    // 操作组集合
    NSSet  *categories = [NSSet setWithObjects:cate, nil];
    
    // 创建通知设置对象
    UIUserNotificationSettings *set = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:categories]; // 添加快捷操作组
    
    // 注册通知设置
    [[UIApplication sharedApplication] registerUserNotificationSettings:set];
    
}
@end
